package net.minecraft.entity.player.Really.Client.utils;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.projectile.EntitySnowball;
import net.minecraft.network.play.client.C03PacketPlayer;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.MathHelper;
import net.minecraft.util.Vec3;

public class RotationUtils {
   public static boolean canEntityBeSeen(Entity e) {
      Vec3 vec1 = new Vec3(Minecraft.thePlayer.posX, Minecraft.thePlayer.posY + (double)Minecraft.thePlayer.getEyeHeight(), Minecraft.thePlayer.posZ);
      AxisAlignedBB box = e.getEntityBoundingBox();
      Vec3 vec2 = new Vec3(e.posX, e.posY + (double)(e.getEyeHeight() / 1.32F), e.posZ);
      double minx = e.posX - 0.25D;
      double maxx = e.posX + 0.25D;
      double miny = e.posY;
      double maxy = e.posY + Math.abs(e.posY - box.maxY);
      double minz = e.posZ - 0.25D;
      double maxz = e.posZ + 0.25D;
      boolean see = Minecraft.theWorld.rayTraceBlocks(vec1, vec2) == null;
      if(see) {
         return true;
      } else {
         vec2 = new Vec3(maxx, miny, minz);
         see = Minecraft.theWorld.rayTraceBlocks(vec1, vec2) == null;
         if(see) {
            return true;
         } else {
            vec2 = new Vec3(minx, miny, minz);
            see = Minecraft.theWorld.rayTraceBlocks(vec1, vec2) == null;
            if(see) {
               return true;
            } else {
               vec2 = new Vec3(minx, miny, maxz);
               see = Minecraft.theWorld.rayTraceBlocks(vec1, vec2) == null;
               if(see) {
                  return true;
               } else {
                  vec2 = new Vec3(maxx, miny, maxz);
                  see = Minecraft.theWorld.rayTraceBlocks(vec1, vec2) == null;
                  if(see) {
                     return true;
                  } else {
                     vec2 = new Vec3(maxx, maxy, minz);
                     see = Minecraft.theWorld.rayTraceBlocks(vec1, vec2) == null;
                     if(see) {
                        return true;
                     } else {
                        vec2 = new Vec3(minx, maxy, minz);
                        see = Minecraft.theWorld.rayTraceBlocks(vec1, vec2) == null;
                        if(see) {
                           return true;
                        } else {
                           vec2 = new Vec3(minx, maxy, maxz - 0.1D);
                           see = Minecraft.theWorld.rayTraceBlocks(vec1, vec2) == null;
                           if(see) {
                              return true;
                           } else {
                              vec2 = new Vec3(maxx, maxy, maxz);
                              see = Minecraft.theWorld.rayTraceBlocks(vec1, vec2) == null;
                              return see;
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }

   public static float[] getBlockRotations(int x2, int y2, int z2, EnumFacing facing) {
      Minecraft mc2 = Minecraft.getMinecraft();
      EntitySnowball temp = new EntitySnowball(Minecraft.theWorld);
      temp.posX = (double)x2 + 0.5D;
      temp.posY = (double)y2 + 0.5D;
      temp.posZ = (double)z2 + 0.5D;
      return getAngles(temp);
   }

   public static float[] getAngles(Entity e2) {
      Minecraft mc2 = Minecraft.getMinecraft();
      return new float[]{getYawChangeToEntity(e2) + Minecraft.thePlayer.rotationYaw, getPitchChangeToEntity(e2) + Minecraft.thePlayer.rotationPitch};
   }

   public static float[] getRotations(Entity entity) {
      if(entity == null) {
         return null;
      } else {
         Minecraft.getMinecraft();
         double diffX = entity.posX - Minecraft.thePlayer.posX;
         Minecraft.getMinecraft();
         double diffZ = entity.posZ - Minecraft.thePlayer.posZ;
         double diffY;
         if(entity instanceof EntityLivingBase) {
            EntityLivingBase elb = (EntityLivingBase)entity;
            Minecraft.getMinecraft();
            Minecraft.getMinecraft();
            diffY = elb.posY + ((double)elb.getEyeHeight() - 0.2D) - (Minecraft.thePlayer.posY + (double)Minecraft.thePlayer.getEyeHeight());
         } else {
            Minecraft.getMinecraft();
            Minecraft.getMinecraft();
            diffY = (entity.boundingBox.minY + entity.boundingBox.maxY) / 2.0D - (Minecraft.thePlayer.posY + (double)Minecraft.thePlayer.getEyeHeight());
         }

         double dist = (double)MathHelper.sqrt_double(diffX * diffX + diffZ * diffZ);
         float yaw = (float)(Math.atan2(diffZ, diffX) * 180.0D / 3.141592653589793D) - 90.0F;
         float pitch = (float)(-Math.atan2(diffY, dist) * 180.0D / 3.141592653589793D) - 60.0F;
         return new float[]{yaw, pitch};
      }
   }

   public static float getYawChange(double posX, double posZ) {
      Minecraft.getMinecraft();
      double deltaX = posX - Minecraft.thePlayer.posX;
      Minecraft.getMinecraft();
      double deltaZ = posZ - Minecraft.thePlayer.posZ;
      double yawToEntity;
      if(deltaZ < 0.0D && deltaX < 0.0D) {
         yawToEntity = 90.0D + Math.toDegrees(Math.atan(deltaZ / deltaX));
      } else if(deltaZ < 0.0D && deltaX > 0.0D) {
         yawToEntity = -90.0D + Math.toDegrees(Math.atan(deltaZ / deltaX));
      } else {
         yawToEntity = Math.toDegrees(-Math.atan(deltaX / deltaZ));
      }

      Minecraft.getMinecraft();
      return MathHelper.wrapAngleTo180_float(-(Minecraft.thePlayer.rotationYaw - (float)yawToEntity));
   }

   public static float getYawChangeToEntity(Entity entity) {
      Minecraft mc2 = Minecraft.getMinecraft();
      double deltaX = entity.posX - Minecraft.thePlayer.posX;
      double deltaZ = entity.posZ - Minecraft.thePlayer.posZ;
      double yawToEntity = deltaZ < 0.0D && deltaX < 0.0D?90.0D + Math.toDegrees(Math.atan(deltaZ / deltaX)):(deltaZ < 0.0D && deltaX > 0.0D?-90.0D + Math.toDegrees(Math.atan(deltaZ / deltaX)):Math.toDegrees(-Math.atan(deltaX / deltaZ)));
      return MathHelper.wrapAngleTo180_float(-Minecraft.thePlayer.rotationYaw - (float)yawToEntity);
   }

   public static float getPitchChangeToEntity(Entity entity) {
      Minecraft mc2 = Minecraft.getMinecraft();
      double deltaX = entity.posX - Minecraft.thePlayer.posX;
      double deltaZ = entity.posZ - Minecraft.thePlayer.posZ;
      double deltaY = entity.posY - 1.6D + (double)entity.getEyeHeight() - 0.4D - Minecraft.thePlayer.posY;
      double distanceXZ = (double)MathHelper.sqrt_double(deltaX * deltaX + deltaZ * deltaZ);
      double pitchToEntity = -Math.toDegrees(Math.atan(deltaY / distanceXZ));
      return -MathHelper.wrapAngleTo180_float(Minecraft.thePlayer.rotationPitch - (float)pitchToEntity);
   }

   public static float[] getRotationFromPosition(double x2, double z2, double y2) {
      Minecraft.getMinecraft();
      double xDiff = x2 - Minecraft.thePlayer.posX;
      Minecraft.getMinecraft();
      double zDiff = z2 - Minecraft.thePlayer.posZ;
      Minecraft.getMinecraft();
      double yDiff = y2 - Minecraft.thePlayer.posY - 0.8D;
      double dist = (double)MathHelper.sqrt_double(xDiff * xDiff + zDiff * zDiff);
      float yaw = (float)(Math.atan2(zDiff, xDiff) * 180.0D / 3.141592653589793D) + 90.0F;
      float pitch = (float)(-Math.atan2(yDiff, dist) * 180.0D / 3.141592653589793D) + 90.0F;
      return new float[]{yaw, pitch};
   }

   public static float getDistanceBetweenAngles(float angle1, float angle2) {
      float angle3 = Math.abs(angle1 - angle2) % 360.0F;
      if(angle3 > 180.0F) {
         angle3 = 0.0F;
      }

      return angle3;
   }

   public static float[] getRotations(Vec3 position) {
      return getRotations(Minecraft.thePlayer.getPositionVector().addVector(0.0D, (double)Minecraft.thePlayer.getEyeHeight(), 0.0D), position);
   }

   public static float[] getRotations(Vec3 origin, Vec3 position) {
      Vec3 difference = position.subtract(origin);
      double distance = difference.flat().lengthVector();
      float yaw = (float)Math.toDegrees(Math.atan2(difference.zCoord, difference.xCoord)) - 90.0F;
      float pitch = (float)(-Math.toDegrees(Math.atan2(difference.yCoord, distance)));
      return new float[]{yaw, pitch};
   }

   public static float[] getRotations(BlockPos pos) {
      return getRotations(Minecraft.thePlayer.getPositionVector().addVector(0.0D, (double)Minecraft.thePlayer.getEyeHeight(), 0.0D), new Vec3((double)pos.getX() + 0.5D, (double)pos.getY() + 0.5D, (double)pos.getZ() + 0.5D));
   }

   public static float[] getBowAngles(Entity entity) {
      double xDelta = entity.posX - entity.lastTickPosX;
      double zDelta = entity.posZ - entity.lastTickPosZ;
      Minecraft.getMinecraft();
      double d2 = (double)Minecraft.thePlayer.getDistanceToEntity(entity);
      d2 = d2 - d2 % 0.8D;
      double xMulti = 1.0D;
      double zMulti = 1.0D;
      boolean sprint = entity.isSprinting();
      xMulti = d2 / 0.8D * xDelta * (sprint?1.25D:1.0D);
      zMulti = d2 / 0.8D * zDelta * (sprint?1.25D:1.0D);
      Minecraft.getMinecraft();
      double x2 = entity.posX + xMulti - Minecraft.thePlayer.posX;
      Minecraft.getMinecraft();
      double z2 = entity.posZ + zMulti - Minecraft.thePlayer.posZ;
      Minecraft.getMinecraft();
      Minecraft.getMinecraft();
      double y2 = Minecraft.thePlayer.posY + (double)Minecraft.thePlayer.getEyeHeight() - (entity.posY + (double)entity.getEyeHeight());
      Minecraft.getMinecraft();
      double dist = (double)Minecraft.thePlayer.getDistanceToEntity(entity);
      float yaw = (float)Math.toDegrees(Math.atan2(z2, x2)) - 90.0F;
      float pitch = (float)Math.toDegrees(Math.atan2(y2, dist));
      return new float[]{yaw, pitch};
   }

   public static float normalizeAngle(float angle) {
      return (angle + 360.0F) % 360.0F;
   }

   public static float getTrajAngleSolutionLow(float d3, float d1, float velocity) {
      float g2 = 0.006F;
      float sqrt = velocity * velocity * velocity * velocity - g2 * (g2 * d3 * d3 + 2.0F * d1 * velocity * velocity);
      return (float)Math.toDegrees(Math.atan(((double)(velocity * velocity) - Math.sqrt((double)sqrt)) / (double)(g2 * d3)));
   }

   public static Vec3 getEyesPos() {
      return new Vec3(Minecraft.thePlayer.posX, Minecraft.thePlayer.posY + (double)Minecraft.thePlayer.getEyeHeight(), Minecraft.thePlayer.posZ);
   }

   public static float[] getRotationsBlock(BlockPos pos) {
      Minecraft mc2 = Minecraft.getMinecraft();
      double d0 = (double)pos.getX() - Minecraft.thePlayer.posX;
      double d1 = (double)pos.getY() - (Minecraft.thePlayer.posY + (double)Minecraft.thePlayer.getEyeHeight());
      double d2 = (double)pos.getZ() - Minecraft.thePlayer.posZ;
      double d3 = (double)MathHelper.sqrt_double(d0 * d0 + d2 * d2);
      float f2 = (float)(MathHelper.atan2(d2, d0) * 180.0D / 3.141592653589793D) - 90.0F;
      float f1 = (float)(-Math.toDegrees(Math.atan2(d3, d1)));
      return new float[]{f1};
   }

   public static float[] getNeededRotations(Vec3 vec) {
      Vec3 eyesPos = getEyesPos();
      double diffX = vec.xCoord - eyesPos.xCoord + 0.5D;
      double diffY = vec.yCoord - eyesPos.yCoord + 0.5D;
      double diffZ = vec.zCoord - eyesPos.zCoord + 0.5D;
      double diffXZ = Math.sqrt(diffX * diffX + diffZ * diffZ);
      float yaw = (float)Math.toDegrees(Math.atan2(diffZ, diffX)) - 90.0F;
      float pitch = (float)(-Math.atan2(diffY, diffXZ) * 180.0D / 3.141592653589793D);
      float[] var10000 = new float[]{MathHelper.wrapAngleTo180_float(yaw), 0.0F};
      Minecraft.getMinecraft();
      var10000[1] = Minecraft.gameSettings.keyBindJump.pressed?90.0F:MathHelper.wrapAngleTo180_float(pitch);
      float[] arrf = var10000;
      return arrf;
   }

   public static void faceVectorPacketInstant(Vec3 vec) {
      float[] rotations = getNeededRotations(vec);
      Minecraft.getMinecraft();
      Minecraft.getMinecraft();
      Minecraft.thePlayer.sendQueue.addToSendQueue(new C03PacketPlayer.C05PacketPlayerLook(rotations[0], rotations[1], Minecraft.thePlayer.onGround));
   }

   public static float getPitchChange(Entity entity, double posY) {
      double var10000 = entity.posX;
      Minecraft.getMinecraft();
      double deltaX = var10000 - Minecraft.thePlayer.posX;
      var10000 = entity.posZ;
      Minecraft.getMinecraft();
      double deltaZ = var10000 - Minecraft.thePlayer.posZ;
      var10000 = posY - 2.2D + (double)entity.getEyeHeight();
      Minecraft.getMinecraft();
      double deltaY = var10000 - Minecraft.thePlayer.posY;
      double distanceXZ = (double)MathHelper.sqrt_double(deltaX * deltaX + deltaZ * deltaZ);
      double pitchToEntity = -Math.toDegrees(Math.atan(deltaY / distanceXZ));
      Minecraft.getMinecraft();
      return -MathHelper.wrapAngleTo180_float(Minecraft.thePlayer.rotationPitch - (float)pitchToEntity) - 2.5F;
   }

   public static float getYawChange(float yaw, double posX, double posZ) {
      Minecraft.getMinecraft();
      double deltaX = posX - Minecraft.thePlayer.posX;
      Minecraft.getMinecraft();
      double deltaZ = posZ - Minecraft.thePlayer.posZ;
      double yawToEntity = 0.0D;
      if(deltaZ < 0.0D && deltaX < 0.0D) {
         if(deltaX != 0.0D) {
            yawToEntity = 90.0D + Math.toDegrees(Math.atan(deltaZ / deltaX));
         }
      } else if(deltaZ < 0.0D && deltaX > 0.0D) {
         if(deltaX != 0.0D) {
            yawToEntity = -90.0D + Math.toDegrees(Math.atan(deltaZ / deltaX));
         }
      } else if(deltaZ != 0.0D) {
         yawToEntity = Math.toDegrees(-Math.atan(deltaX / deltaZ));
      }

      return MathHelper.wrapAngleTo180_float(-(yaw - (float)yawToEntity));
   }

   public static float getPitchChange(float pitch, Entity entity, double posY) {
      double var10000 = entity.posX;
      Minecraft.getMinecraft();
      double deltaX = var10000 - Minecraft.thePlayer.posX;
      var10000 = entity.posZ;
      Minecraft.getMinecraft();
      double deltaZ = var10000 - Minecraft.thePlayer.posZ;
      var10000 = posY - 2.2D + (double)entity.getEyeHeight();
      Minecraft.getMinecraft();
      double deltaY = var10000 - Minecraft.thePlayer.posY;
      double distanceXZ = (double)MathHelper.sqrt_double(deltaX * deltaX + deltaZ * deltaZ);
      double pitchToEntity = -Math.toDegrees(Math.atan(deltaY / distanceXZ));
      return -MathHelper.wrapAngleTo180_float(pitch - (float)pitchToEntity) - 2.5F;
   }
}
